package com.yidiandao.business.ui.widget.listview.displaylistitem;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.imagemodule.SecondKill;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.url.UrlActivity;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.viewholder.HomeSecondKillHolder;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/** 首页-天天秒杀 ListItem */
public class HomeSecondKillListItem extends DisplayListItem<HomeSecondKillHolder, SecondKill>
        implements View.OnClickListener {

    private Goods mGoods1;
    private Goods mGoods2;
    private Goods mGoods3;

    private Activity mActivity;
    private String mUrl;

    public HomeSecondKillListItem(SecondKill s) {
        super(s);
    }

    @Override
    public void onShow(Context context, HomeSecondKillHolder holder) {
        mActivity = (Activity) context;
        mUrl = data.getUrl();
        SecondKill.TimeInfo timeInfo = data.getTimeInfo();

        if (timeInfo != null) {
            holder.refreshTitle(timeInfo.getTitle());
            holder.timerLayout.setLeaveTime(data.getLeaveTime());
        } else {
            holder.refreshTitle(UIUtils.getString(R.string.all_second_kill_over));
            holder.timerLayout.unTimer();
        }

        holder.tvEnter.setOnClickListener(this);

        List<Goods> goodsList = data.getGoodsList();
        try {
            mGoods1 = goodsList.get(0);
            holder.refreshUI1(mGoods1);
            holder.tvNewPrice1.setOnClickListener(this);
            holder.ivImage1.setOnClickListener(this);

            mGoods2 = goodsList.get(1);
            holder.rootLayout2.setVisibility(View.VISIBLE);
            holder.refreshUI2(mGoods2);
            holder.tvNewPrice2.setOnClickListener(this);
            holder.ivImage2.setOnClickListener(this);

            mGoods3 = goodsList.get(2);
            holder.rootLayout3.setVisibility(View.VISIBLE);
            holder.refreshUI3(mGoods3);
            holder.tvNewPrice3.setOnClickListener(this);
            holder.ivImage3.setOnClickListener(this);

        } catch (IndexOutOfBoundsException e) {
            if (mGoods2 == null) {
                holder.rootLayout2.setVisibility(View.INVISIBLE);
            }
            if (mGoods3 == null) {
                holder.rootLayout3.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public String getDisplayItemTag() {
        return HomeSecondKillListItem.class.getSimpleName();
    }

    @Override
    public Class<HomeSecondKillHolder> getViewHolderClazz() {
        return HomeSecondKillHolder.class;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.tv_new_price_1:
//            case R.id.tv_new_price_2:
//            case R.id.tv_new_price_3:
//                if (!PriceUtils.isLogin()) {
//                    LoginActivity.actionStart(mActivity);
//                }
//                break;
            case R.id.iv_image_1:
                toGoodsDetailActivity(mGoods1);
                break;
            case R.id.iv_image_2:
                toGoodsDetailActivity(mGoods2);
                break;
            case R.id.iv_image_3:
                toGoodsDetailActivity(mGoods3);
                break;

            case R.id.tv_enter:
                UrlActivity.actionStart(mActivity, mUrl);
                break;
        }
    }

    private void toGoodsDetailActivity(Goods goods) {
        GoodsDetailActivity.actionStart(mActivity, goods.getGoodsId());
    }
}
